#!/bin/bash

#获取当前时间
datetime=`date "+%Y-%m-%d %H:%M:%S"`

#读取IP列表行数
lineNumber=`cat iplist.txt |wc -l`

#设置计数参数
count=0

#清理历史ping测试记录结果
> ./telnetdown.txt

cat ./iplist.txt | while read line; do
    #计数器
    count=$((count+1))
    #控制台打印当前进度
    echo "${line} ${count}/${lineNumber}"
    #3秒之后telnet没有连到对端IP端口，执行kill -9终止telnet命令
    result=`echo -e '\x1dclose\x0d' | timeout --signal=9 3 telnet $line 2>/dev/null | grep -Eo "Connected|refused"`
    if [ "$result" == "Connected" ]; then
        echo "${datetime}|${line} Port Open" >> ./telnetdown.txt

    elif [ "$result" == "refused" ]; then
        echo "${datetime}|${line} Connection refused" >> ./telnetdown.txt

    else
        echo "${datetime}|${line} Port Closed" >> ./telnetdown.txt
    fi
don